import { Component, OnInit } from '@angular/core';
import { MenuItem } from 'primeng/primeng';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { CommonService } from '../common/service/common.service';
import { switchMap } from 'rxjs/operators';
import { SYSTEM_TITLE } from '../common/configuration';

@Component({
  templateUrl: './notice.component.html',
  styleUrls: ['./notice.component.scss']
})

export class NoticeComponent implements OnInit {
    sysId: number;
    subTitle: string;
    systemTitle: string;
    sysIcon: string;
    items: MenuItem[];
    notice: any = {};

  constructor(
      private router: Router,
      private route: ActivatedRoute,
      private commonService: CommonService) {

  }

  ngOnInit() {
      this.systemTitle = SYSTEM_TITLE;
      let afterUrl = decodeURI(window.location.pathname).substring(1);
      let strings = afterUrl.split('/');
      let url = strings[0];
      if (url != null || url != '') {
          this.commonService.getInitSystemByUrl(url).subscribe(data => {
              this.subTitle = data.sysName;
              this.sysIcon = '/assets/images/desktop/' + data.iconImage + '.png';
          });
      } else {
          this.commonService.getInitSystemByUrl("noticefication").subscribe(data => {
              this.subTitle = data.sysName;
              this.sysIcon = '/assets/images/desktop/' + data.iconImage + '.png';
          });
      }

      this.route.paramMap.pipe(
          switchMap((params: ParamMap) => {
              this.sysId = +params.get('sysId') || 12;
              this.notice = params.get('notice') || {};
              return this.commonService.initsystemresource(this.sysId);
          })
      ).subscribe(data => {
          this.items = data.menus;
          let resources = (localStorage.getItem('resources') || '').split(',');
          localStorage.setItem('resources', resources.concat(data.resIds).join(','));
      });
  }

}
